package com.guyu.plugin.oss.dao;

import com.alibaba.fastjson2.JSON;
import com.guyu.plugin.oss.constant.OssConstant;
import com.guyu.plugin.oss.properties.OssProperties;
import com.guyu.plugin.redisson.util.CacheUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;


@Slf4j
public class OssDaoDefaultImpl implements OssDao {

    private static volatile OssDaoDefaultImpl INSTANCE;

    /**
     * 获取唯一实例
     *
     * @return 唯一实例
     */
    public static OssDaoDefaultImpl getInstance() {
        if (null == INSTANCE) {
            synchronized (OssDaoDefaultImpl.class) {
                if (null == INSTANCE) {
                    INSTANCE = new OssDaoDefaultImpl();
                }
            }
        }
        return INSTANCE;
    }


    @Override
    public void set(String key, OssProperties value) {
        CacheUtils.put(OssConstant.SYS_OSS_CONFIG, key, JSON.toJSONString(value));
    }

    @Override
    public OssProperties get(String key) {
        String json = CacheUtils.get(OssConstant.SYS_OSS_CONFIG, key);
        return JSON.parseObject(json, OssProperties.class);
    }

    @Override
    public void remove(String key) {
        CacheUtils.evict(OssConstant.SYS_OSS_CONFIG, key);
    }

    @Override
    public void clean() {
        CacheUtils.clear(OssConstant.SYS_OSS_CONFIG);
    }

    @Override
    public Set<Object> keys() {
        return CacheUtils.keys(OssConstant.SYS_OSS_CONFIG);
    }

}
